﻿using System;
using System.Collections.Generic;

namespace Leaf.NetCore.Cache
{
    /// <summary>
    /// 版本：1.0 描述：缓存工厂类 日期：2018-11-5 17:48:05 创建人：王志海 Copyright (c) 2015-2018 首溯科技有限公司
    /// </summary>
    public class CacheHelper
    {
        /// <summary>
        /// 定义通用的Repository
        /// </summary>
        /// <returns></returns>
        public static ICache Cache(CacheEnumType databaseType = CacheEnumType.ConfigSet, string redisconfig = "")
        {
            ICache result = null;
            string cacheType = "WebCache";
            //if (databaseType == CacheEnumType.ConfigSet)
            //{
            //    cacheType = LeafAgileConfig.Get("CacheType");
            //}
            //else
            //{
            //    cacheType = databaseType.ToString();
            //}

            switch (cacheType)
            {
                case "Redis":
                    result = new RedisCache(redisconfig);
                    break;

                case "WebCache":
                    result = new Cache();
                    break;

                default:
                    result = new Cache();
                    break;
            }
            return result;
        }

        /// <summary>
        /// 获取缓存值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="databaseType"></param>
        /// <returns></returns>
        public static T GetCache<T>(string cacheKey, CacheEnumType databaseType = CacheEnumType.ConfigSet) where T : class
        {
            return Cache(databaseType).GetCache<T>(cacheKey);
        }

        /// <summary>
        /// 删除缓存值
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="databaseType"></param>
        public static void RemoveCache(string cacheKey, CacheEnumType databaseType = CacheEnumType.ConfigSet)
        {
            Cache(databaseType).RemoveCache(cacheKey);
        }

        /// <summary>
        /// 删除所有缓存
        /// </summary>
        /// <param name="databaseType"></param>
        public static void RemoveCache(CacheEnumType databaseType = CacheEnumType.ConfigSet)
        {
            Cache(databaseType).RemoveCache();
        }

        /// <summary>
        /// 设置缓存值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="cacheValue"></param>
        /// <param name="expireTime"></param>
        /// <param name="databaseType"></param>
        public static void SetCache<T>(string cacheKey, T cacheValue, DateTime? expireTime = null, CacheEnumType databaseType = CacheEnumType.ConfigSet) where T : class
        {
            Cache(databaseType).SetCache<T>(cacheKey, cacheValue, expireTime == null ? DateTime.Now.AddHours(1) : expireTime);
        }

        /// <summary>
        /// 获取所有key值
        /// </summary>
        /// <param name="databaseType"></param>
        /// <returns></returns>
        public static List<string> GetKeys(CacheEnumType databaseType = CacheEnumType.ConfigSet)
        {
            return Cache(databaseType).GetKeys();
        }
    }

    /// <summary>
    /// 缓存分类
    /// </summary>
    public enum CacheEnumType
    {
        /// <summary>
        /// 配置文件
        /// </summary>
        ConfigSet,

        /// <summary>
        /// Redis
        /// </summary>
        Redis,

        /// <summary>
        /// 系统缓存
        /// </summary>
        WebCache
    }
}